package com.yj.web.controllers.admin;

import com.yj.web.annotation.LoginRequired;
import com.yj.web.dao.po.admin.*;
import com.yj.web.model.BaseWebResult;
import com.yj.web.service.AdminUserService;
import com.yj.web.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by yanlinyang on 2017/7/4.
 */
@Controller
@RequestMapping("/admin")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private MessageService messageService;

    @LoginRequired
    @RequestMapping(value = "/listUser", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listUser(@RequestParam("nickname") String nickname,
                           @RequestParam("idNumber") String idNumber,
                           @RequestParam("phone") String phone,
                           @RequestParam("startTime") String startTime,
                           @RequestParam("endTime") String endTime,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<AdminUser> users;
        try {
            users = adminUserService.listUserPage(nickname, idNumber, phone, startTime, endTime, page, size);
            result.setData(users);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }
    @LoginRequired
    @RequestMapping(value = "/countUser", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countUser(@RequestParam("nickname") String nickname,
                                                   @RequestParam("idNumber") String idNumber,
                                                   @RequestParam("phone") String phone,
                                                   @RequestParam("startTime") String startTime,
                                                   @RequestParam("endTime") String endTime) {
        BaseWebResult result = new BaseWebResult();
        int count = adminUserService.countUser(nickname, idNumber, phone, startTime, endTime);
        result.setData(count);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/listAllUser", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listUser(@RequestParam("nickname") String nickname,
                                                @RequestParam("idNumber") String idNumber,
                                                @RequestParam("phone") String phone,
                                                @RequestParam("startTime") String startTime,
                                                @RequestParam("endTime") String endTime) {
        BaseWebResult result = new BaseWebResult();
        List<AdminUser> users;
        try {
            users = adminUserService.listUser(nickname, idNumber, phone, startTime, endTime);
            result.setData(users);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    /**
     *
     * @param id 用户编号，对应cus_user.user_no
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/findUserById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findUserById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            AdminUser user = adminUserService.findUserById(id);
            result.setData(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findAccountInfoById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findAccountInfoById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            Map accountInfo = adminUserService.findAccountInfoById(id);
            result.setData(accountInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/changeBankCardNo", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult changeBankCardNo(@RequestParam("id") String id, @RequestParam("bankCardNo") String bankCardNo) {
        BaseWebResult result = new BaseWebResult();
        try {
            adminUserService.setBankCardNo(id, bankCardNo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findInvestRecordById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findInvestRecordById(@RequestParam("id") String id, @RequestParam(value = "page", defaultValue = "1") int page,
                                                            @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<InvestRecord> investRecords = adminUserService.findInvestRecordById(id, page, size);
            if (null != investRecords) {
                result.setData(investRecords);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countInvestRecordById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countInvestRecordById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            int num = adminUserService.countInvestRecordById(id);
            result.setData(num);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    //筹款记录
    @LoginRequired
    @RequestMapping(value = "/findFundraisingRecordById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findFundraisingRecordById(@RequestParam("id") String id, @RequestParam(value = "page", defaultValue = "1") int page,
                                                                 @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<FundraisingRecord> fundraisingRecords = adminUserService.findFundraisingRecordById(id, page, size);
//            if (null != fundraisingRecords && fundraisingRecords.get(0) != null) {
                result.setData(fundraisingRecords);
//            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countFundraisingRecordById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countFundraisingRecordById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            int num = adminUserService.countFundraisingRecordById(id);
            result.setData(num);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    //资金流水
    @LoginRequired
    @RequestMapping(value = "/findCapitalChangeById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findCapitalChangeById(@RequestParam("id") String id, @RequestParam(value = "page", defaultValue = "1") int page,
                                                             @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<CapitalChange> capitalChanges = adminUserService.findCapitalChangeById(id, page, size);
            if (null != capitalChanges) {
                result.setData(capitalChanges);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countCapitalChangeById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countCapitalChangeById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            int num = adminUserService.countCapitalChangeById(id);
            result.setData(num);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/listMessageByReceiver", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listMessageByReceiver(@RequestParam("phone") String phone, @RequestParam(value = "page", defaultValue = "1") int page,
                                                       @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        try {
            List<Message> msgs = messageService.listMessageByReceiver(phone, page, size);
            if (null != msgs) {
                result.setData(msgs);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/countMessageByReceiver", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countMessageByReceiver(@RequestParam("phone") String phone) {
        BaseWebResult result = new BaseWebResult();
        try {
            int num = messageService.countMessageByReceiver(phone);
            result.setData(num);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/sendMessage", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult sendMessage(@ModelAttribute("message") Message message) {
        BaseWebResult result = new BaseWebResult();
        try {
            messageService.insertMessage(message);
            result.setMsg("增加成功");
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/modifyMsgStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult modifyMsgStatus(@RequestParam("id") long id) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = messageService.updateMessage(id);
            if (true != flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }


    /**
     *
     * @param id cus_account_detail.id
     * @return
     */
    @LoginRequired
    @RequestMapping(value="/modifyCapitalChangeStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult modifyCapitalChangeStatus(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = adminUserService.updateCapitalChange(id);
            if (true != flag) {
                result.setCd(1);
                result.setMsg("系统错误");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    //提现
    @LoginRequired
    @RequestMapping(value = "/listDepositWithdraw", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult listDepositWithdraw(@RequestParam("realName") String realName,
                                                @RequestParam("startTime") String startTime,
                                                @RequestParam("endTime") String endTime, @RequestParam("phone") String phone, @RequestParam("status") Integer status,
                                                @RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<DepositWithdraw> depositWithdraws;
        try {
            depositWithdraws = adminUserService.listDepositWithdraw(realName, startTime, endTime, phone, status, page, size);
            result.setData(depositWithdraws);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }
    @LoginRequired
    @RequestMapping(value = "/countDepositWithdraw", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countDepositWithdraw(@RequestParam("realName") String realName,
                                                            @RequestParam("startTime") String startTime,
                                                            @RequestParam("endTime") String endTime,
                                                            @RequestParam("phone") String phone,
                                                            @RequestParam("status") Integer status) {
        BaseWebResult result = new BaseWebResult();
        int count = adminUserService.countDepositWithdraw(realName, startTime, endTime, phone, status);
        result.setData(count);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/findDepositWithdrawById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult findDepositWithdrawById(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            DepositWithdraw depositWithdraw = adminUserService.findDepositWithdrawById(id);
            result.setData(depositWithdraw);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateDepositWithdrawStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateDepositWithdrawStatus(@RequestParam("id") String id) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = adminUserService.updateDepositWithdrawStatus(id);
            if (true == flag) {
                result.setMsg("放款成功");
            } else {
                result.setCd(1);
                result.setMsg("放款失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }
}
